package com.villaday.idf.adapter3;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.villaday.idf.R;

import com.villaday.idf.custom.bannerUtil.AbsRecyclerViewAdapter;
import com.villaday.idf.model3.GuestPostEntity;

public class OrderInputAdapter extends AbsRecyclerViewAdapter<GuestPostEntity> {


    View.OnClickListener onClickListener;
    private onAddTextChangeLisnter onAddTextChangeLisnter;
    private boolean isBind = true;

    public OrderInputAdapter(Context context, View.OnClickListener onClickListener) {
        super(context, com.villaday.idf.R.layout.recycle_order_input);
        this.onClickListener = onClickListener;
    }

    @Override
    public void onBindHolder(RecyclerViewHolder holder, GuestPostEntity d, final int position) {
        try {
            final EditText   etCard = (EditText) holder.getView(R.id.et_oi_card);
            final EditText etHome = (EditText) holder.getView(R.id.et_oi_home);
            final EditText etGive = (EditText) holder.getView(R.id.et_oi_give);
            TextView tvline = (TextView) holder.getView(R.id.tv_line);

            if (d.isAllowCn()) {
                etGive.setVisibility(View.GONE);
                tvline.setVisibility(View.GONE);
                etHome.setHint("房间" + (position + 1) + "，每间需填一人姓名");
                etCard.setHint("房间" + (position + 1) + "，身份证号");
                if (d.getNameCn() != null) {
                    etHome.setText(d.getNameCn());
                } else {
                    etHome.setText("");
                }
            } else {
                etGive.setVisibility(View.VISIBLE);
                tvline.setVisibility(View.VISIBLE);
                etHome.setHint("姓(如 HAN)");
                etGive.setHint("名(如 MEIMEI)");
                etCard.setHint("房间" + (position + 1) + "，身份证号");
                if (d.getGivenNameEn() != null) {
                    etGive.setText(d.getGivenNameEn());
                } else {
                    etGive.setText("");
                }

                if (d.getFamilyNameEn() != null) {
                    etHome.setText(d.getFamilyNameEn());
                } else {
                    etHome.setText("");
                }
            }


            //判定是否为特殊需要增加身份证
            if (d.isAllowIdentityNumber()) {
                etCard.setVisibility(View.VISIBLE);

                if (d.getIdcard() != null) {
                    etCard.setText(d.getIdcard());
                } else {
                    etCard.setText("");
                }


            } else {
                if (data.size() == 1 || position == data.size() - 1) {
                    holder.getView(R.id.vw_line).setVisibility(View.GONE);
                } else {
                    holder.getView(R.id.vw_line).setVisibility(View.VISIBLE);
                }

                etCard.setVisibility(View.GONE);
            }


            if ((data.size() - 1) == position) {
                holder.getView(R.id.vw_line_bottom).setVisibility(View.GONE);
            } else {
                holder.getView(R.id.vw_line_bottom).setVisibility(View.VISIBLE);
            }

            ImageView imageView = (ImageView) holder.getView(com.villaday.idf.R.id.iv_oi_home);
            imageView.setTag(position);
            imageView.setOnClickListener(onClickListener);

            if (position == 0) {
                imageView.setVisibility(View.VISIBLE);
            } else {
                imageView.setVisibility(View.GONE);
            }



            if ((etHome.getTag()!=null&&(Boolean) etHome.getTag())||(etGive.getTag()!=null&&(Boolean) etGive.getTag())
                    ||etCard.getTag()!=null&&(Boolean) etCard.getTag()){
                return;
            }

            etGive.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                }

                @Override
                public void afterTextChanged(Editable editable) {
                    onAddTextChangeLisnter.textChange(etGive, editable.toString(), position);
                }
            });


            etCard.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                }

                @Override
                public void afterTextChanged(Editable editable) {
                    onAddTextChangeLisnter.textChange(etCard, editable.toString(), position);
                }
            });

            etHome.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                }

                @Override
                public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                }

                @Override
                public void afterTextChanged(Editable editable) {
                    onAddTextChangeLisnter.textChange(etHome, editable.toString(), position);
                }
            });
            etHome.setTag(isBind);
            etGive.setTag(isBind);
            etCard.setTag(isBind);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public interface onAddTextChangeLisnter {
        void textChange(EditText editText, String text, int position);
    }


    public void setOnAddTextChangeLisnter(onAddTextChangeLisnter onAddTextChangeLisnter) {
        this.onAddTextChangeLisnter = onAddTextChangeLisnter;
    }


}
